﻿package javase.io;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class DomXmlParse {

	public static void main(String[] args) {
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(new File("src/com/javase/xml/demo/FirstXml.xml"));
			/** 去除空白的TestNode */
			doc.normalize();

			NodeList nlist = doc.getElementsByTagName("title");
			Node node = nlist.item(0);

			/** 也可以用Element,Element继承了Node */
			// Element e = (Element)nlist.item(0);
//			node.removeChild(node.getFirstChild());
			String str = node.getFirstChild().getNodeValue();
			System.out.println("title:" + str);
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
